/*
 * @Author: 何 海深 813936564@qq.com
 * @Date: 2022-08-10 18:45:21
 * @LastEditors: 何 海深 813936564@qq.com
 * @LastEditTime: 2023-12-01 20:13:15
 * @FilePath: \show-data-vue3\src\router\permission.ts
 * @Description: 路由拦截处理
 */
import router from './index'
import { commonStore } from '@/stores/modules/common'
import stores from '@/stores/index'
import { exitSystemJump } from '@/hooks/userOutLogin/userOutLoginIndex'
const store = commonStore(stores)
router.beforeEach(async (to: any, from: any, next: any) => {
  // console.log("路由", to);
  next()
  return
  const token = localStorage.getItem('token')
  if (to.path === '/error' && !token) {
    next()
  }
  if (to.query && to.query.token) {
    localStorage.setItem('token', to.query.token)
    const res: any = await store.isAuthed().catch((err: any) => {
      console.log('页面错误', err)
      // 跳转到错误页
      // err && next({ path: "/error" });
      // 跳转登录页
      err && exitSystemJump()
      return false
    })
    if (res) {
      if (location.toString().includes('?token=')) {
        next({ path: to.path })
      } else {
        next()
      }
    } else {
      console.log('登录失败', res)
      // next({ path: "/error" });
      exitSystemJump()
      return false
    }
  } else {
    const token = localStorage.getItem('token')
    if (!store.$state.userInfo) {
      const res = await store.isAuthed()
      // console.log('yijingdegnlu', res)
      if (!res) {
        // console.log('yijingdegnlu2', res, router, router.getRoutes(), to)
        localStorage.removeItem('token')
        // next("/error");
        exitSystemJump()
        // next();
        return false
      }
    } else {
      next()
    }
    /* console.log("获取的token", token); */
    if (token) {
      /*  console.log("ggggg", to); */

      next()
    } else {
      exitSystemJump()
      // next({ path: "/error" });
    }
  }
})
router.afterEach(async () => {
  console.log('路由跳转后')
})
